/**
 * Created by gm on 2016/8/9.
 */
var express=require('express');
var router=express.Router();
var crypto=require('crypto');

var monk=require('monk');
var db=monk('localhost:27017/usercollection');
var collection=db.get('administrator');

router.get('/',function(req,res){
  var pageSize=parseInt(req.query.pageSize);
  var skip=parseInt((req.query.p_current-1)*pageSize);
    if(req.query.content==undefined||req.query.content==""){
      collection.count({},function(err,data){
        if(err) throw err;
        collection.find({},{sort:{_id:-1},skip:skip,limit:pageSize},function(err,result){
          if(err) throw err;
          // console.log(result);
          var jsonArray={count:data,result:result};
          res.json(jsonArray);
        });
      });
    }else{
      var con=new RegExp(req.query.content);
      console.log(con);
      collection.count({"$or":[{appid:con},{username:con}]},function(err,data){
        if(err) throw err;
        collection.find({"$or":[{appid:con},{username:con}]},{sort:{_id:-1},skip:skip,limit:pageSize},function(err,result){
          if(err) throw err;
          var jsonArray={count:data,result:result};
          res.json(jsonArray);
        });
      });
    }

});

router.post('/',function(req,res){
    collection.insert({
        username:req.body.username,
        password:req.body.password
    },function(err,data){
        if(err) throw err;
        res.json(data);
    });
});

router.get('/getuser/:id',function(req,res){
    collection.findOne({_id:req.params.id},function(err,data){
        if(err) throw err;
        res.json(data);
    });
});

router.post('/login',function(req,res){
    var username=req.body.username;
    collection.findOne({username:username},function(err,data){
        if(err) throw err;
        if(data!=null){
            res.json(data);
        }else{
            res.json({msg:"用户名有误，请重新输入"});
        }
    });
});

router.post('/update',function(req,res){
    collection.update({
            _id:req.body._id
        },
        {$set:
        {password:req.body.password}
        },function(err,data){
            if(err) throw err;
            res.json(data);
        });
});

router.delete('/:id',function(req,res){
    collection.remove({_id:req.params.id},function(err,data){
        if(err) throw err;
        res.json(data);
    });
});

router.post('/addsuser',function(req,res){
  var data=req.body;
  console.log(data);
  var md5 = crypto.createHash('md5');
  md5.update(data.password);
  var d = md5.digest('hex');
  console.log(d);
  var md52=crypto.createHash('md5');
  md52.update("c7vq89kh" +d);
  var psw=md52.digest('hex');
  console.log(psw);
  data.password=psw;
  console.log(data);
  if(data!=null){
    collection.findOne({username:data.username},function(err,result){
      if(err) throw err;
      if(result==null){
        collection.insert({
          username:data.username,
          password:data.password,
          appid:data.appid
        },function(err,errresult) {
          if (err) throw err;
          res.json(errresult);
        });
      }else{
        if(data.username==data.username){
          res.json({errNum:0,message:"商户号重复，请重新输入"});
        }
      }
    });
  }
});
module.exports=router;